﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Kernel;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SysLicense
    {
        private readonly KeRui.DAL.SysLicense dal = new KeRui.DAL.SysLicense();

        public int Add(KeRui.Model.SysLicense model)
        {
            model = this.CheckActivate(model);
            return this.dal.Add(model);
        }

        internal KeRui.Model.SysLicense CheckActivate(KeRui.Model.SysLicense model)
        {
            model.IsActivated = Software.Activated(model.Domain, model.ServerIP, model.LicenseCode) ? 1 : 0;
            return model;
        }

        public List<KeRui.Model.SysLicense> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SysLicense> list = new List<KeRui.Model.SysLicense>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SysLicense model = new KeRui.Model.SysLicense();
                    if (dt.Rows[i]["LicenseID"].ToString() != "")
                    {
                        model.LicenseID = int.Parse(dt.Rows[i]["LicenseID"].ToString());
                    }
                    model.Domain = dt.Rows[i]["Domain"].ToString();
                    model.ServerIP = dt.Rows[i]["ServerIP"].ToString();
                    model.LicenseCode = dt.Rows[i]["LicenseCode"].ToString();
                    if (dt.Rows[i]["IsActivated"].ToString() != "")
                    {
                        model.IsActivated = int.Parse(dt.Rows[i]["IsActivated"].ToString());
                    }
                    if (dt.Rows[i]["ActivateTime"].ToString() != "")
                    {
                        model.ActivateTime = DateTime.Parse(dt.Rows[i]["ActivateTime"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        model.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    model = this.CheckActivate(model);
                    list.Add(model);
                }
            }
            return list;
        }

        public int Delete(int LicenseID)
        {
            return this.dal.Delete(LicenseID);
        }

        public List<KeRui.Model.SysLicense> GetList(int IsActivated)
        {
            string strWhere = "";
            if ((IsActivated == 0) || (IsActivated == 1))
            {
                strWhere = "IsActivated=" + IsActivated;
            }
            string strOrder = "LicenseID asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("SysLicense", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SysLicense> GetListByCache(int IsActivated)
        {
            string cacheKey = "SysLicenseList-" + IsActivated;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(IsActivated);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SysLicense>) cache;
        }

        public KeRui.Model.SysLicense GetModel(int LicenseID)
        {
            KeRui.Model.SysLicense model = this.dal.GetModel(LicenseID);
            return this.CheckActivate(model);
        }

        public KeRui.Model.SysLicense GetModel(string Domain, string ServerIP)
        {
            KeRui.Model.SysLicense model = this.dal.GetModel(Domain, ServerIP);
            return this.CheckActivate(model);
        }

        public KeRui.Model.SysLicense GetModelByCache(int LicenseID)
        {
            string cacheKey = "SysLicenseModel-" + LicenseID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(LicenseID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SysLicense) cache;
        }

        public KeRui.Model.SysLicense GetModelByCache(string Domain, string ServerIP)
        {
            string cacheKey = "SysLicenseModel-" + Domain + "-" + ServerIP;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(Domain, ServerIP);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SysLicense) cache;
        }

        public List<KeRui.Model.SysLicense> GetServerDomain(string ServerIP)
        {
            string strWhere = "ServerIP='" + ServerIP + "' AND IsActivated=1";
            string strOrder = "LicenseID desc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("SysLicense", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public int Update(KeRui.Model.SysLicense model)
        {
            model = this.CheckActivate(model);
            return this.dal.Update(model);
        }
    }
}

